home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume7 / safe < prev    next >
Encoding:
Internet Message Format  |  1986-11-30  |  5.4 KB

  1. Subject:  v07i032:  Limit a program's execution time
  2. Newsgroups: mod.sources
  3. Approved: mirror!rs
  4.  
  5. Submitted by: ihnp4!aicchi!mdb
  6. Mod.sources: Volume 7, Issue 32
  7. Archive-name: safe
  8.  
  9. [ Between an amazingly massive work crunch, and the replacement or exchange
  10.   of both our minicomputers, there has been no mod.sources postings for almost
  11.   a month.  Sorry.  For this program, I wrote a Makefile and tweaked the
  12.   manpage a bit.  --r$ ]
  13.  
  14. I thought I'd send this in since there has been some discussion about this
  15. sort of program.  I have tested/used it on SYS V r2.  It *should* be portable,
  16. though.
  17.  
  18.                     Mike Blackwell
  19.                     ...!ihnp4!aicchi!mdb    (USENET)
  20.  
  21. #!/bin/sh
  22. # This is a shell archive.  Remove anything before this line,
  23. # then unpack it by saving it in a file and typing "sh file".
  24. # If all goes well, you will see the message "No problems found."
  25.  
  26. # Exit status; set to 1 on "wc" errors or if would overwrite.
  27. STATUS=0
  28. # Contents:  Makefile safe.1 safe.c
  29.  
  30. echo x - Makefile
  31. if test -f Makefile ; then
  32.     echo Makefile exists, putting output in $$Makefile
  33.     OUT=$$Makefile
  34.     STATUS=1
  35. else
  36.     OUT=Makefile
  37. fi
  38. sed 's/^X//' > $OUT <<'@//E*O*F Makefile//'
  39. X##  MAKEFILE FOR 'SAFE'
  40.  
  41. XCFLAGS    = -O
  42.  
  43. X##  THIS PROGRAM USES GETOPT; TWEAK THE NEXT LINE AS NECESSARY FOR YOU
  44. X#GETOPT    = -lgetopt
  45.  
  46. Xsafe:    safe.c
  47. X    cc $(CFLAGS) -o safe $(GETOPT)
  48.  
  49.  
  50. Xinstall:
  51. X    @echo Copy safe and the manpage to wherever is appropriate
  52. @//E*O*F Makefile//
  53. chmod u=rw,g=rw,o=rw $OUT
  54.  
  55. echo x - safe.1
  56. if test -f safe.1 ; then
  57.     echo safe.1 exists, putting output in $$safe.1
  58.     OUT=$$safe.1
  59.     STATUS=1
  60. else
  61.     OUT=safe.1
  62. fi
  63. sed 's/^X//' > $OUT <<'@//E*O*F safe.1//'
  64. X.TH SAFE 1L
  65. X.SH NAME
  66. Xsafe \- kill a process after specified time.
  67. X.SH SYNOPSIS
  68. X.B safe -d <delay> -w <warning> -b command_line
  69. X.SH DESCRIPTION
  70. X.I Safe
  71. Xexecutes the given
  72. X.BR command_line ,
  73. Xkilling the process if it is still running after
  74. X.B delay
  75. Xseconds.
  76. XIf the -w option is specified, a warning will be given
  77. X.B warning
  78. Xseconds before the process is to be terminated.
  79. XThe -b option supresses the text of the warning message and only beeps the
  80. Xterminal; this is useful if you are in a screen oriented program.
  81. X.PP
  82. XI wrote this program to make it easier to test a program that was doing
  83. Xsome strange ioctls.
  84. XHowever it seems to have many uses, including
  85. Xcontrolling netnews addiction.
  86. X.SH AUTHOR
  87. XMike Blackwell  -  USENET:  ...!ihnp4!aicchi!mdb
  88. X.SH NOTES
  89. XThe program traps SIGINT and uses it to terminate the process.
  90. XI haven't run into any problems with this, but it could be a problem with
  91. Xsome programs.
  92.  
  93. @//E*O*F safe.1//
  94. chmod u=rw,g=rw,o=rw $OUT
  95.  
  96. echo x - safe.c
  97. if test -f safe.c ; then
  98.     echo safe.c exists, putting output in $$safe.c
  99.     OUT=$$safe.c
  100.     STATUS=1
  101. else
  102.     OUT=safe.c
  103. fi
  104. sed 's/^X//' > $OUT <<'@//E*O*F safe.c//'
  105. X/* safe - Mike Blackwell - Aug 1986 */
  106. X/* safe.c - safeguard a program: kill it after `n` seconds if its still alive */
  107.  
  108. X#include <signal.h>
  109. X#include <stdio.h>
  110.  
  111. X/* delay constants */
  112. X#define MINDELAY    10
  113. X#define MAXDELAY    60*60
  114. X#define DEFDELAY    60
  115. X#define DLYMSG        "safe: <delay> must be between %d and %d seconds\n"
  116. X#define WARNING        "\007\007safe: killing process %d in %d seconds\n"
  117. X#define BELL        "\007\007"
  118. X#define USAGE        "Usage:\n\tsafe -d <delay> -b -w <warning>\n"
  119.  
  120. Xint pid;
  121.  
  122. Xmain(argc,argv)
  123. Xint argc;
  124. Xchar *argv[];
  125. X{
  126. X    int done(),maim();
  127. X    unsigned int delay = DEFDELAY;
  128. X    unsigned int warndelay = 0;
  129. X    int opt;
  130. X    extern char *optarg;
  131. X    extern int optind;
  132. X    short int bell = 0;
  133.  
  134. X    while ((opt = getopt(argc,argv,"d:bw:")) != EOF) {
  135. X        switch (opt) {
  136. X        case 'd':
  137. X            sscanf(optarg,"%d",&delay);
  138. X            if (delay < MINDELAY || delay > MAXDELAY) {
  139. X                fprintf(stderr,DLYMSG,MINDELAY,MAXDELAY);
  140. X                exit(-1);
  141. X            }
  142. X            break;
  143. X        case 'b':
  144. X            bell = 1;
  145. X            break;
  146. X        case 'w':
  147. X            sscanf(optarg,"%d",&warndelay);
  148. X            break;
  149. X        case '?':
  150. X            fprintf(stderr,USAGE);
  151. X            exit(-1);
  152. X            break;
  153. X        }
  154. X    }
  155.  
  156. X    if (argv[optind] == NULL) {
  157. X        fprintf(stderr,"safe: no program to execute\n");
  158. X        exit(1);
  159. X    }
  160.  
  161. X    if (warndelay >= delay) {
  162. X        fprintf(stderr,
  163. X      "safe: delay less than warning time, no warning will be given\n");
  164. X        warndelay = 0;
  165. X    }
  166.  
  167. X    if (delay > warndelay) {
  168. X        delay -= warndelay;
  169. X    }
  170.  
  171. X    pid = fork();
  172. X    if (pid == -1) {
  173. X        fprintf(stderr,"safe: Can't fork\n");
  174. X        perror("safe");
  175. X        exit(-1);
  176. X    }
  177. X    if (pid == 0) {        /* child process */
  178. X        if (execvp(argv[optind],&argv[optind]) == -1) {
  179. X            perror("safe");
  180. X            exit(-1);
  181. X        }
  182. X    } else {        /* parent - monitor and kill */
  183. X        signal(SIGCLD,done);
  184. X        signal(SIGINT,maim);
  185. X        sleep(delay);
  186. X        if (warndelay != 0) {
  187. X            if (bell)
  188. X                fprintf(stderr,BELL);
  189. X            else
  190. X                fprintf(stderr,WARNING,pid,warndelay);
  191. X            sleep(warndelay);
  192. X        }
  193. X        maim();
  194. X        exit(1);
  195. X    }
  196. X}
  197.  
  198. Xmaim()
  199. X{
  200. X    signal(SIGCLD,SIG_IGN);
  201. X    kill(pid,SIGKILL);
  202. X    fprintf(stderr,"safe: Process %d killed \n",pid);
  203. X    exit(1);
  204. X}
  205.  
  206. Xdone()
  207. X{
  208. X    fprintf(stderr,"safe: Process terminated\n");
  209. X    exit(0);
  210. X}
  211. @//E*O*F safe.c//
  212. chmod u=rw,g=rw,o=rw $OUT
  213.  
  214. echo Inspecting for damage in transit...
  215. temp=/tmp/sharin$$; dtemp=/tmp/sharout$$
  216. trap "rm -f $temp $dtemp; exit" 0 1 2 3 15
  217. cat > $temp <<\!!!
  218.       13      41     245 Makefile
  219.       29     159     908 safe.1
  220.      106     269    2041 safe.c
  221.      148     469    3194 total
  222. !!!
  223. wc  Makefile safe.1 safe.c | sed 's=[^ ]*/==' | diff -b $temp - >$dtemp
  224. if test -s $dtemp ; then
  225.     echo "Ouch [diff of wc output]:"
  226.     cat $dtemp
  227.     STATUS=1
  228. elif test $STATUS = 0 ; then
  229.     echo "No problems found."
  230. else
  231.     echo "WARNING -- PROBLEMS WERE FOUND..."
  232. fi
  233. exit $STATUS
  234.